package com.wxm.common.file.start.config;

import com.alibaba.fastjson.JSONObject;
import com.wxm.common.file.core.config.UploadConfigService;
import com.wxm.common.file.core.config.UploadConfigVo;
import com.wxm.common.file.core.constant.YesNoConstant;
import com.wxm.common.file.core.property.*;
import com.wxm.common.file.core.result.DeleteFileResult;
import com.wxm.common.file.core.result.UploadFileResult;
import com.wxm.common.file.core.result.UploadMd5Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.function.Consumer;
import java.util.function.Function;

/**
 * <p>
 *      上传配置
 * </p>
 *
 * @author: wxm
 * @Date: 2022/1/20
 */
@Slf4j
@Service
public class UploadConfigServiceImpl implements UploadConfigService {
    @Override
    public UploadConfigVo getUploadConfigVo(JSONObject uploadParam) {
        UploadConfigVo uploadConfigVo = new UploadConfigVo();
        uploadConfigVo.setMaxSize(100 * 1024L); //最大上传
        uploadConfigVo.setRenTag(YesNoConstant.YES);//开启重命名

        TencentCloudCosProperty aliyunOssProperty = new TencentCloudCosProperty();
        aliyunOssProperty.setSecretId("");
        aliyunOssProperty.setSecretKey("");
        aliyunOssProperty.setRegion("");
        aliyunOssProperty.setBucket("roco");
        aliyunOssProperty.setRootPath("http://roco.oss-cn-beijing.aliyuncs.com");

        uploadConfigVo.setUploadBaseProperty(aliyunOssProperty);
        return uploadConfigVo;
    }

    @Override
    public Consumer<UploadFileResult> uploadAfter(JSONObject uploadParam) {
        return item -> {
            log.info("文件上传成功: {}",item.getAccessPath());
        };
    }

    @Override
    public Function<JSONObject,Object> deleteAfter() {
        return item ->{
            log.info("文件删除成功: {}",item);
            return 1;
        };
    }

    @Override
    public boolean checkFileMd5() {
        //开启MD5校验
        return false;
    }

    @Override
    public UploadMd5Result doCheckFileMd5(String nowFileMd5,JSONObject uploadParam) {
        log.info("比对文件MD5信息: {}",nowFileMd5);
        UploadMd5Result result = new UploadMd5Result();
        result.setKey(1);
        return result;
    }

    @Override
    public Consumer<UploadMd5Result> md5EqAfter() {
        return item ->{
            log.info("MD5文件比对成功: {} 更新上传时间",item);
        };
    }
}
